package com.example.smartcity1.my;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.example.smartcity1.R;
import com.example.smartcity1.adapter.MyOrderAdapter;
import com.example.smartcity1.bean.MyOrderBean;
import com.example.smartcity1.network.RetrofitClient;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MyUserOrderActivity extends AppCompatActivity {

    private static final String TAG = "MyUserOrderActivity";

    private RadioGroup myOrderRadioGroup;
    private RadioButton myOrderRadio1;
    private RadioButton myOrderRadio2;
    private ListView myOrderList;
    List<MyOrderBean.DataDTO> dataStatus0;
    List<MyOrderBean.DataDTO> dataStatus1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_user_order);
        ActionBar supportActionBar = getSupportActionBar();
        if (supportActionBar != null) {
            supportActionBar.setTitle("订单列表");
            supportActionBar.setDisplayHomeAsUpEnabled(true);
        }
        initView();
        initListData();
        myOrderRadio1.setClickable(false);
        myOrderRadio2.setClickable(false);
        myOrderRadioGroup.setOnCheckedChangeListener((group, checkedId) -> {
            switch (checkedId) {
                case R.id.my_order_radio1:
                    MyOrderAdapter adapter1 = new MyOrderAdapter(MyUserOrderActivity.this,R.layout.item_my_order_all,dataStatus0);
                    myOrderList.setAdapter(adapter1);
                    break;
                case R.id.my_order_radio2:
                    MyOrderAdapter adapter2 = new MyOrderAdapter(MyUserOrderActivity.this,R.layout.item_my_order_all,dataStatus1);
                    myOrderList.setAdapter(adapter2);
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    public boolean onSupportNavigateUp() {
        finish();
        return super.onSupportNavigateUp();
    }


    private void initView() {
        myOrderRadioGroup = (RadioGroup) findViewById(R.id.my_order_radioGroup);
        myOrderRadio1 = (RadioButton) findViewById(R.id.my_order_radio1);
        myOrderRadio2 = (RadioButton) findViewById(R.id.my_order_radio2);
        myOrderList = (ListView) findViewById(R.id.my_order_list);
    }

    private void initListData() {
        SharedPreferences user = getSharedPreferences("user", MODE_PRIVATE);
//        ,user.getString("userId","1")
        RetrofitClient.appService().getMyOrderData(user.getString("token","")).enqueue(new Callback<MyOrderBean>() {
            @Override
            public void onResponse(Call<MyOrderBean> call, Response<MyOrderBean> response) {
                MyOrderBean myOrderBean = response.body();
                if (myOrderBean != null && myOrderBean.getCode() == 200) {
                    List<MyOrderBean.DataDTO> listData =myOrderBean.getData();
                    dataStatus0 = new ArrayList<>();
                    dataStatus1 = new ArrayList<>();
                    for (MyOrderBean.DataDTO data: listData) {
                        if (data.getStatus() == 0) {
                            dataStatus0.add(data);
                        } else {
                            dataStatus1.add(data);
                        }
                    }
                    MyOrderAdapter adapter1 = new MyOrderAdapter(MyUserOrderActivity.this,R.layout.item_my_order_all,dataStatus0);
                    myOrderList.setAdapter(adapter1);
                    myOrderRadio1.setClickable(true);
                    myOrderRadio2.setClickable(true);
                }
            }
            @Override
            public void onFailure(Call<MyOrderBean> call, Throwable t) {
            }
        });
    }
}